/*
 * Copyright 2006 Jeremias Maerki
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Text;

namespace ZXing.Datamatrix.Encoder
{
    /// <summary>
    /// Symbol info table for DataMatrix.
    /// </summary>
    public class SymbolInfo
    {
        internal static readonly SymbolInfo[] PROD_SYMBOLS = {
                                                         new SymbolInfo(false, 3, 5, 8, 8, 1),
                                                         new SymbolInfo(false, 5, 7, 10, 10, 1),
                                                         /*rect*/new SymbolInfo(true, 5, 7, 16, 6, 1),
                                                         new SymbolInfo(false, 8, 10, 12, 12, 1),
                                                         /*rect*/new SymbolInfo(true, 10, 11, 14, 6, 2),
                                                         new SymbolInfo(false, 12, 12, 14, 14, 1),
                                                         /*rect*/new SymbolInfo(true, 16, 14, 24, 10, 1),

                                                         new SymbolInfo(false, 18, 14, 16, 16, 1),
                                                         /*rect*/new SymbolInfo(true, 18, 15, 22, 6, 2, true), // DMRE 8x48 (18)
                                                         new SymbolInfo(false, 22, 18, 18, 18, 1),
                                                         /*rect*/new SymbolInfo(true, 22, 18, 16, 10, 2),
                                                         /*rect*/new SymbolInfo(true, 24, 18, 14, 6, 4, true), // DMRE 8x64 (24)
                                                         new SymbolInfo(false, 30, 20, 20, 20, 1),
                                                         /*rect*/new SymbolInfo(true, 32, 24, 16, 14, 2),
                                                         /*rect*/new SymbolInfo(true, 32, 22, 18, 6, 4, true), // DMRE 8x80 (32)
                                                         new SymbolInfo(false, 36, 24, 22, 22, 1),
                                                         /*rect*/new SymbolInfo(true, 38, 28, 22, 6, 4, true), // DMRE 8x96 (38)
                                                         /*rect*/new SymbolInfo(true, 43, 27, 14, 10, 4, true), // DMRE 12x64 (43)
                                                         new SymbolInfo(false, 44, 28, 24, 24, 1),
                                                         /*rect*/new SymbolInfo(true, 44, 28, 16, 18, 2, true), // DMRE 20x36 (44)
                                                         /*rect*/new SymbolInfo(true, 49, 28, 22, 14, 2),
                                                         /*rect*/new SymbolInfo(true, 49, 32, 18, 6, 6, true), // DMRE 8x120 (49)
                                                         /*rect*/new SymbolInfo(true, 56, 34, 20, 18, 2, true), // DMRE 20x44 (56)

                                                         new SymbolInfo(false, 62, 36, 14, 14, 4),
                                                         /*rect*/new SymbolInfo(true, 62, 36, 14, 14, 4, true), // DMRE 16x64 (62)
                                                         /*rect*/new SymbolInfo(true, 63, 36, 22, 6, 6, true), // DMRE 8x144 (63)
                                                         /*rect*/new SymbolInfo(true, 64, 36, 20, 10, 4, true), // DMRE 12x88 (64)
                                                         /*rect*/new SymbolInfo(true, 70, 38, 18, 24, 2, true), // DMRE 26x40 (70)
                                                         /*rect*/new SymbolInfo(true, 72, 38, 22, 20, 2, true), // DMRE 22x48 (72)
                                                         /*rect*/new SymbolInfo(true, 80, 41, 22, 22, 2, true), // DMRE 24x48 (80)
                                                         /*rect*/new SymbolInfo(true, 84, 42, 14, 18, 4, true), // DMRE 20x64 (84)
                                                         new SymbolInfo(false, 86, 42, 16, 16, 4),
                                                         /*rect*/new SymbolInfo(true, 90, 42, 22, 24, 2, true), // DMRE 26x48 (90)
                                                         /*rect*/new SymbolInfo(true, 108, 46, 14, 22, 4, true), // DMRE 24x64 (108)
                                                         new SymbolInfo(false, 114, 48, 18, 18, 4),
                                                         /*rect*/new SymbolInfo(true, 118, 50, 14, 24, 4, true), // DMRE 26x64 (118)
                                                         new SymbolInfo(false, 144, 56, 20, 20, 4),
                                                         new SymbolInfo(false, 174, 68, 22, 22, 4),

                                                         new SymbolInfo(false, 204, 84, 24, 24, 4, 102, 42),
                                                         new SymbolInfo(false, 280, 112, 14, 14, 16, 140, 56),
                                                         new SymbolInfo(false, 368, 144, 16, 16, 16, 92, 36),
                                                         new SymbolInfo(false, 456, 192, 18, 18, 16, 114, 48),
                                                         new SymbolInfo(false, 576, 224, 20, 20, 16, 144, 56),
                                                         new SymbolInfo(false, 696, 272, 22, 22, 16, 174, 68),
                                                         new SymbolInfo(false, 816, 336, 24, 24, 16, 136, 56),
                                                         new SymbolInfo(false, 1050, 408, 18, 18, 36, 175, 68),
                                                         new SymbolInfo(false, 1304, 496, 20, 20, 36, 163, 62),
                                                         new DataMatrixSymbolInfo144(),
                                                      };

        private static SymbolInfo[] symbols = PROD_SYMBOLS;

        private readonly bool rectangular;
        internal readonly int dataCapacity;
        internal readonly int errorCodewords;
        /// <summary>
        /// matrix width
        /// </summary>
        public readonly int matrixWidth;
        /// <summary>
        /// matrix height
        /// </summary>
        public readonly int matrixHeight;
        private readonly int dataRegions;
        private readonly int rsBlockData;
        private readonly int rsBlockError;
        private readonly bool dmre;

        /**
         * Overrides the symbol info set used by this class. Used for testing purposes.
         *
         * @param override the symbol info set to use
         */
        public static void overrideSymbolSet(SymbolInfo[] @override)
        {
            symbols = @override;
        }
        /// <summary>
        /// initializing constructor
        /// </summary>
        /// <param name="rectangular"></param>
        /// <param name="dataCapacity"></param>
        /// <param name="errorCodewords"></param>
        /// <param name="matrixWidth"></param>
        /// <param name="matrixHeight"></param>
        /// <param name="dataRegions"></param>
        public SymbolInfo(bool rectangular, int dataCapacity, int errorCodewords,
                          int matrixWidth, int matrixHeight, int dataRegions)
           : this(rectangular, dataCapacity, errorCodewords, matrixWidth, matrixHeight, dataRegions,
                dataCapacity, errorCodewords)
        {
        }
        public SymbolInfo(bool rectangular, int dataCapacity, int errorCodewords,
                          int matrixWidth, int matrixHeight, int dataRegions, bool dmre = false)
           : this(rectangular, dataCapacity, errorCodewords, matrixWidth, matrixHeight, dataRegions,
                dataCapacity, errorCodewords, dmre)
        {
        }

        internal SymbolInfo(bool rectangular, int dataCapacity, int errorCodewords,
                           int matrixWidth, int matrixHeight, int dataRegions,
                           int rsBlockData, int rsBlockError, bool dmre = false)
        {
            this.rectangular = rectangular;
            this.dataCapacity = dataCapacity;
            this.errorCodewords = errorCodewords;
            this.matrixWidth = matrixWidth;
            this.matrixHeight = matrixHeight;
            this.dataRegions = dataRegions;
            this.rsBlockData = rsBlockData;
            this.rsBlockError = rsBlockError;
            this.dmre = dmre;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataCodewords"></param>
        /// <returns></returns>
        public static SymbolInfo lookup(int dataCodewords)
        {
            return lookup(dataCodewords, SymbolShapeHint.FORCE_NONE, true);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataCodewords"></param>
        /// <param name="shape"></param>
        /// <returns></returns>
        public static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape)
        {
            return lookup(dataCodewords, shape, true);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataCodewords"></param>
        /// <param name="allowRectangular"></param>
        /// <param name="fail"></param>
        /// <returns></returns>
        public static SymbolInfo lookup(int dataCodewords, bool allowRectangular, bool fail)
        {
            SymbolShapeHint shape = allowRectangular
                ? SymbolShapeHint.FORCE_NONE : SymbolShapeHint.FORCE_SQUARE;
            return lookup(dataCodewords, shape, fail);
        }

        private static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape, bool fail)
        {
            return lookup(dataCodewords, shape, null, null, fail);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataCodewords"></param>
        /// <param name="shape"></param>
        /// <param name="minSize"></param>
        /// <param name="maxSize"></param>
        /// <param name="fail"></param>
        /// <returns></returns>
        public static SymbolInfo lookup(int dataCodewords,
                                        SymbolShapeHint shape,
                                        Dimension minSize,
                                        Dimension maxSize,
                                        bool fail)
        {
            foreach (SymbolInfo symbol in symbols)
            {
                if (symbol.dmre)
                    // DMRE Symbols doesn't work in all cases
                    continue;

                if (shape == SymbolShapeHint.FORCE_SQUARE && symbol.rectangular)
                {
                    continue;
                }
                if (shape == SymbolShapeHint.FORCE_RECTANGLE && !symbol.rectangular)
                {
                    continue;
                }
                if (minSize != null
                    && (symbol.getSymbolWidth() < minSize.Width
                    || symbol.getSymbolHeight() < minSize.Height))
                {
                    continue;
                }
                if (maxSize != null
                    && (symbol.getSymbolWidth() > maxSize.Width
                    || symbol.getSymbolHeight() > maxSize.Height))
                {
                    continue;
                }
                if (dataCodewords <= symbol.dataCapacity)
                {
                    return symbol;
                }
            }
            if (fail)
            {
                throw new ArgumentException(
                    "Can't find a symbol arrangement that matches the message. Data codewords: "
                        + dataCodewords);
            }
            return null;
        }

        private int getHorizontalDataRegions()
        {
            if (dmre)
            {
                return dataRegions;
            }

            switch (dataRegions)
            {
                case 1:
                    return 1;
                case 2:
                case 4:
                    return 2;
                case 16:
                    return 4;
                case 36:
                    return 6;
                default:
                    throw new InvalidOperationException("Cannot handle this number of data regions");
            }
        }

        private int getVerticalDataRegions()
        {
            if (dmre)
            {
                return 1;
            }

            switch (dataRegions)
            {
                case 1:
                case 2:
                    return 1;
                case 4:
                    return 2;
                case 16:
                    return 4;
                case 36:
                    return 6;
                default:
                    throw new InvalidOperationException("Cannot handle this number of data regions");
            }
        }

        /// <summary>
        /// symbol data width
        /// </summary>
        /// <returns></returns>
        public int getSymbolDataWidth()
        {
            return getHorizontalDataRegions() * matrixWidth;
        }
        /// <summary>
        /// symbol data height
        /// </summary>
        /// <returns></returns>
        public int getSymbolDataHeight()
        {
            return getVerticalDataRegions() * matrixHeight;
        }
        /// <summary>
        /// symbol width
        /// </summary>
        /// <returns></returns>
        public int getSymbolWidth()
        {
            return getSymbolDataWidth() + (getHorizontalDataRegions() * 2);
        }
        /// <summary>
        /// symbol height
        /// </summary>
        /// <returns></returns>
        public int getSymbolHeight()
        {
            return getSymbolDataHeight() + (getVerticalDataRegions() * 2);
        }
        /// <summary>
        /// codeword count
        /// </summary>
        /// <returns></returns>
        public int getCodewordCount()
        {
            return dataCapacity + errorCodewords;
        }
        /// <summary>
        /// interleaved block count
        /// </summary>
        /// <returns></returns>
        public virtual int getInterleavedBlockCount()
        {
            return dataCapacity / rsBlockData;
        }
        /// <summary>
        /// data length for interleaved block
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public virtual int getDataLengthForInterleavedBlock(int index)
        {
            return rsBlockData;
        }
        /// <summary>
        /// error length for interleaved block
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int getErrorLengthForInterleavedBlock(int index)
        {
            return rsBlockError;
        }

        /// <summary>
        /// user friendly representation
        /// </summary>
        /// <returns></returns>
        public override String ToString()
        {
            var sb = new StringBuilder();
            sb.Append(rectangular ? "Rectangular Symbol:" : "Square Symbol:");
            sb.Append(" data region ").Append(matrixWidth).Append('x').Append(matrixHeight);
            sb.Append(", symbol size ").Append(getSymbolWidth()).Append('x').Append(getSymbolHeight());
            sb.Append(", symbol data size ").Append(getSymbolDataWidth()).Append('x').Append(getSymbolDataHeight());
            sb.Append(", codewords ").Append(dataCapacity).Append('+').Append(errorCodewords);
            return sb.ToString();
        }
    }
}